<?php
/**
 * 扩展函数文件
 */
defined('IN_IXDCW') or exit('Access Denied');

/**
 * 分类比较函数
 * @param $a
 * @param $b
 * @return int
 */
function xd_catsort($a, $b) {
	return strcmp($a['letter'], $b['letter']);
}


/**
 * 生成唯一的UID
 * @return string
 */
function xd_guid() {
	if (function_exists('com_create_guid')) {
		return com_create_guid();
	} else {
		mt_srand((double) microtime() * 10000); //optional for php 4.2.0 and up.
		$charid = strtoupper(md5(uniqid(rand(), true)));
		$hyphen = chr(45); // "-"
		$uuid   = chr(123) // "{"
			. substr($charid, 0, 8) . $hyphen
			. substr($charid, 8, 4) . $hyphen
			. substr($charid, 12, 4) . $hyphen
			. substr($charid, 16, 4) . $hyphen
			. substr($charid, 20, 12)
			. chr(125);
		// "}"
		return $uuid;
	}
}

/**
 * 限定模块权限.
 * @param $moduleid
 * @return int
 */
function xd_areaAuth($moduleid) {
	global $MG, $MODULE;
	$module = $MODULE[$moduleid]['module'];
	if (isset($MG['city_' . $module])) {
		$auth = $MG['city_' . $module];
	} else {
		$auth = -1;
	}
	return $auth;
}

/**
 * 使用PHP实现动态传参生成URL
 * @param string $name      名称
 * @param string $value     值
 * @param array  $buildBase 基准数组
 * @return string
 * @deprecated
 */
function xd_dynamic_url($name = '', $value = '', array $buildBase = array()) {
	$arrUrl = array();
	if (is_string($name)) {
		$buildBase[] = $name;
		$buildBase   = array_unique($buildBase);
	} else if (is_array($name)) {
		foreach ($name as $v) {
			$buildBase[] = $v;
		}
		$buildBase = array_unique($buildBase);
	}

	if ($buildBase) {
		foreach ($buildBase as $v) {
			if (is_string($name)) {
				if ($v == $name) {
					if (!empty($value) || $value == 0)
						$arrUrl[] = "{$name}=" . rawurlencode($value);
				} else {
					if (isset($GLOBALS[$v]) && !empty($GLOBALS[$v])) {
						$arrUrl[] = "{$v}=" . rawurlencode($GLOBALS[$v]);
					}
				}
			} else if (is_array($name)) {
				if (in_array($v, $name)) {
					$key = array_search($v, $name);
					$val = isset($value[$key]) ? $value[$key] : 0;
					if (!empty($value[$key]))
						$arrUrl[] = "{$name[$key]}=" . rawurlencode($val);
				} else {
					if (isset($GLOBALS[$v]) && !empty($GLOBALS[$v])) {
						$arrUrl[] = "{$v}=" . rawurlencode($GLOBALS[$v]);
					}
				}
			}

		}
	}
	if ($arrUrl) {
		return '?' . implode('&', $arrUrl);
	} else {
		return '?';
	}
}

function reUrl($filter, $var = '', $value = null) {
	$base = Env::nquri();
	if (!empty($var)) {
		if (is_string($var)) {
			$filter[$var] = $value;
		} else if (is_array($var)) {
			foreach ($var as $fk => $fv) {
				$filter[$fv] = isset($value[$fk]) ? $value[$fk] : null;
			}
		}
	}
	$qs = !empty($filter) ? '?' . http_build_query($filter) : '';
	return $base . $qs;
}

function xd_area_select($areaid = 0, $buildBase = array()) {
	$AREA = $GLOBALS['AREA'];
	if (!$areaid || !$AREA[$areaid]) {
		$parentid     = 0;
		$areaid       = 0;
		$arrparentids = array();
	} else {
		$parentid     = $AREA[$areaid]['parentid'];
		$arrparentids = explode(',', $AREA[$areaid]['arrparentid']);
	}
	$content = '';
	foreach ($arrparentids as $k => $pid) {
		$display = isset($arrparentids[$k + 1]) ? $AREA[$arrparentids[$k + 1]]['areaname'] : $AREA[$areaid]['areaname'];
		$content .= '<dl class="magic"><dt>' . $display . '</dt>';

		// 获取子元素
		$childs = array();
		foreach ($AREA as $v) {
			if ($v['parentid'] == $pid) {
				$childs[] = $v;
			}
		}
		$content .= '<dd class="clear" style="display:none"><a href="' . xd_dynamic_url('areaid', $pid, $buildBase) . '">不限地区</a>';
		foreach ($childs as $area) {
			$content .= '<a href="' . xd_dynamic_url('areaid', $area['areaid'], $buildBase) . '" title="' . $area['areaname'] . '">' . $area['areaname'] . '</a>';
		}
		$content .= '</dd></dl>';
	}
	if ($AREA[$areaid]['child'] || ($parentid == 0 && !$areaid)) {
		$content .= '<dl class="magic"><dt>不限地区</dt>';
		// 获取子元素
		$childs = array();
		foreach ($AREA as $v) {
			if ($v['parentid'] == $areaid) {
				$childs[] = $v;
			}
		}
		$content .= '<dd class="clear" style="display:none"><a href="' . xd_dynamic_url('areaid', $areaid, $buildBase) . '">不限地区</a>';
		foreach ($childs as $area) {
			$content .= '<a href="' . xd_dynamic_url('areaid', $area['areaid'], $buildBase) . '" title="' . $area['areaname'] . '">' . $area['areaname'] . '</a>';
		}
		$content .= '</dd></dl>';
	}


	return $content;
}

/**
 * 重构过的
 */
function xd_rf_area_select($areaid = 0, $buildBase = array()) {
	$AREA = $GLOBALS['AREA'];
	if (!$areaid || !$AREA[$areaid]) {
		$parentid     = 0;
		$areaid       = 0;
		$arrparentids = array();
	} else {
		$parentid     = $AREA[$areaid]['parentid'];
		$arrparentids = explode(',', $AREA[$areaid]['arrparentid']);
	}
	$content = '';
	foreach ($arrparentids as $k => $pid) {
		$display = isset($arrparentids[$k + 1]) ? $AREA[$arrparentids[$k + 1]]['areaname'] : $AREA[$areaid]['areaname'];
		$content .= '<ul><li class="first"><a href="#">' . $display . '</a><em class="triangle"></em></li>';

		// 获取子元素
		$childs = array();
		foreach ($AREA as $v) {
			if ($v['parentid'] == $pid) {
				$childs[] = $v;
			}
		}
		$content .= '<li class="second fn-hide"><ul class="place fn-clear"><li><a href="' . xd_dynamic_url('areaid', $pid, $buildBase) . '">不限地区</a></li>';
		foreach ($childs as $area) {
			$content .= '<li><a href="' . xd_dynamic_url('areaid', $area['areaid'], $buildBase) . '" title="' . $area['areaname'] . '">' . $area['areaname'] . '</a></li>';
		}
		$content .= '</ul></li></ul>';
	}
	if ($AREA[$areaid]['child'] || ($parentid == 0 && !$areaid)) {
		$content .= '<ul><li class="first"><a href="#">不限地区</a><em class="triangle"></em></li>';
		// 获取子元素
		$childs = array();
		foreach ($AREA as $v) {
			if ($v['parentid'] == $areaid) {
				$childs[] = $v;
			}
		}
		$content .= '<li class="second fn-hide"><ul class="place fn-clear"><li><a href="' . xd_dynamic_url('areaid', $areaid, $buildBase) . '">不限地区</a></li>';
		foreach ($childs as $area) {
			$content .= '<li><a href="' . xd_dynamic_url('areaid', $area['areaid'], $buildBase) . '" title="' . $area['areaname'] . '">' . $area['areaname'] . '</a></li>';
		}
		$content .= '</ul></li></ul>';
	}


	return $content;
}

/**
 * 生成模块的访问地址
 * url('auto/piclist');
 * @param $dirWithAction
 * @param $param
 * @return string
 */
function url($dirWithAction, $param = '') {
	$url = SITE_URL . $dirWithAction . '.php';
	if (!empty($param)) {
		if (is_array($param)) {
			$param = '?' . http_build_query($param);
		} else {
			$param = '?' . $param;
		}
	}
	return $url . ($param ? $param : '');
}

/**
 * 生成URL的请求地址
 * @param       $moduleWithAction
 * @param array $param
 * @return string
 */
function ajaxUrl($moduleWithAction, $param = array()) {
	if (strpos($moduleWithAction, '/') === false) {
		$action = strval($moduleWithAction);
		$module = 'common';
	} else {
		$params = explode('/', $moduleWithAction);
		$module = strval($params[0]);
		$action = strval($params[1]);
	}
	if (!empty($param)) {
		if (is_array($param)) {
			$param = '&' . http_build_query($param);
		} else {
			$param = '&' . $param;
		}
	}
	$url = SITE_URL . 'ajax.php?module=' . $module . '&action=' . $action . ($param ? $param : '');
	return $url;
}

function apiUrl($moduleWithAction, $param = array()) {
	if (strpos($moduleWithAction, '/') === false) {
		$action = strval($moduleWithAction);
		$module = 'default';
	} else {
		$params = explode('/', $moduleWithAction);
		$module = strval($params[0]);
		$action = strval($params[1]);
	}
	if (!empty($param)) {
		if (is_array($param)) {
			$param = '&' . http_build_query($param);
		} else {
			$param = '&' . $param;
		}
	}
	$url = SITE_URL . 'api.php?aM=' . $module . '&aA=' . $action . ($param ? $param : '');
	return $url;
}

/**
 * 显示页面URL
 * @param              $itemid
 * @param string       $show
 * @param mixed|string $module
 * @return string
 */
function showUrl($itemid, $show = 'show', $module = MODULE_NAME) {
	return url($module . '/' . $show, array('itemid' => $itemid));
}

/**
 * 列表页面URL
 * @param              $param
 * @param string       $list
 * @param mixed|string $module
 * @return string
 */
function listUrl($param, $list = 'list', $module = MODULE_NAME) {
	return url($module . '/' . $list, $param);
}

/**
 * dz的获取变量的方式, 弃用extract
 * @param        $k
 * @param null   $default
 * @param string $type
 * @return null
 */
function getgpc($k, $default = null, $type = 'GP') {
	$type = strtoupper($type);

	switch ($type) {
		case 'G':
			$var = & $_GET;

			break;
		case 'P':
			$var = & $_POST;
			break;

		case 'C':
			$var = & $_COOKIE;
			break;
		default:
			if (isset($_GET[$k])) {
				$var = & $_GET;

			} else {
				$var = & $_POST;
			}
			break;
	}
	return isset($var[$k]) ? $var[$k] : $default; //注意这里没有设置的时候返回的是NULL.
}

/**
 * 获取头像
 * @param        $user
 * @param string $size
 * @return string
 */
function getAvatar($user, $size = '24') {
	$MOD = PCache::read('module-2.php');
	include_once DT_ROOT . '/api/uc.inc.php';
	if (!is_numeric($size)) {
		$size = '24';
	}
	if (uc_check_avatar($user['userid'])) {
		$url = $MOD['uc_api'] . '/avatar.php?uid=' . $user['userid'];
	} else {
		$url = 'http://www.gravatar.com/avatar/' . md5(strtolower($user['email'])) . '?s=' . $size;
	}
	$avatar = "<img src='{$url}' /> ";
	return $avatar;
}

/**
 * todo 未完善
 * @param $sqlBuilder
 * @return mixed
 * @author Mark Zhao
 */
function result($sqlBuilder) {
	return ActiveRecord\Cache::get($sqlBuilder, function () use ($sqlBuilder) {
		echo $sqlBuilder;
		return 53;
	});
}